//
//  UpdateAvatarController.m
//  Job
//
//  Created by 如此的你 on 2018/8/21.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "UpdateAvatarController.h"

@interface UpdateAvatarController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *avatarImage;

@end

@implementation UpdateAvatarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改头像";

    [self.avatarImage sd_setImageWithURL:[NSURL URLWithString:self.user.portrait] placeholderImage:[UIImage imageNamed:@"def_header"]];
    
    [self.avatarImage setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseImage)];
    [self.avatarImage addGestureRecognizer:tap];
    
}

- (void)chooseImage {
    [self openAlertControllerTitle:@"选择图片" message:nil style:UIAlertControllerStyleActionSheet actionTitle1:@"相册" actionTitle2:@"拍照" callback:^(int index) {
        if (index == 1) {
            [self openImagePickerWithTarget:self withType:UIImagePickerControllerSourceTypePhotoLibrary editing:YES];
        } else if (index == 2) {
            [self openImagePickerWithTarget:self withType:UIImagePickerControllerSourceTypeCamera editing:YES];
        }
    }];
}

/** 照片选择完成的代理方法，照片信息保存在info参数中 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self dismissViewControllerAnimated:YES completion:nil];    
    [self uploadImage:image];
    
    self.avatarImage.image = image;

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)uploadImage:(UIImage *)imageData {
    
    XLAuthModel *auth = [XLAuthModel getAuth];
    MBProgressHUD *hud = [MBProgressHUD showMessage:@"更新头像中..."];
    NSString *url = [NSString stringWithFormat:@"%@/api/user/images/%@",domain,auth.userID];
    AFHTTPSessionManager *manager = [HTTPTools manager];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"image_name"] = @"portraitURL";
    [manager POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data = UIImageJPEGRepresentation(imageData, 0.5);
        [formData appendPartWithFileData:data name:@"file" fileName:@"file.jpg" mimeType:@"image/jpeg"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        [MBProgressHUD showSuccess:@"更新成功!"];
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            self.user.portrait = responseObject[@"path"];
            [XLUserModel saveUserInfo:self.user];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        [hud hideAnimated:YES];
    }];
}





@end
